//
//  PlayDetailsViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/5.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "PlayDetailsViewController.h"
#import "PlayList.h"
#import "UMSocial.h"
#import "DataBaseManager.h"
#import "MBProgressHUD.h"

@interface PlayDetailsViewController ()<UIWebViewDelegate>
@property(nonatomic, retain)UIWebView *webView;
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation PlayDetailsViewController
-(void)dealloc
{
    [_webView release];
    [_progressView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *collectButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang1"] style:UIBarButtonItemStylePlain target:self action:@selector(collectButtonAction)];
    UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fenxiang"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonAction)];
    shareButton.tintColor = [UIColor purpleColor];
    collectButton.tintColor = [UIColor purpleColor];

    NSArray *array = [NSArray arrayWithObjects: shareButton, collectButton, nil];
    //将数组赋值给rightBarButtonItems
    self.navigationItem.rightBarButtonItems = array;
    
    _flag = 0;
    //检测是否已经收藏
    NSArray *allArray = [DataBaseManager selectPlayList];
    //遍历已经收藏的所有
    for (PlayList *play in allArray) {
        if ([play.product_id isEqualToString:[NSString stringWithFormat:@"%@", _play.product_id]]) {
            [self.navigationItem.rightBarButtonItems[1] setImage:[UIImage imageNamed:@"shoucang2"]];
            _flag = 1;
            break;
        }
    }
}

-(void)loadView
{
    [super loadView];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _webView.delegate = self;
    [self.view addSubview:_webView];
    [_webView release];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://web.breadtrip.com/hunter/product/%@/", _play.product_id]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    
    //加载视图初始化
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_progressView removeFromSuperview];
}

-(void)shareButtonAction
{
    NSArray *array = [NSArray arrayWithObject:UMShareToSina];
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:nil shareImage:nil shareToSnsNames:array delegate:self];
}
-(void)collectButtonAction
{
    //创建表格
    [DataBaseManager createPlayTable];
    //插入数据到数据表
//    [DataBaseManager insertPlayList:_play];
    //删除
//    [DataBaseManager deletePlayList:_play.product_id];
    
    if (_flag == 0) {
        [DataBaseManager insertPlayList:_play];
        [self.navigationItem.rightBarButtonItems[1]setImage:[UIImage imageNamed:@"shoucang2"]];
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"收藏成功"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        _flag = 1;
    }
    else {
        [DataBaseManager deletePlayList:[NSString stringWithFormat:@"%@", _play.product_id]];
        [self.navigationItem.rightBarButtonItems[1]setImage:[UIImage imageNamed:@"shoucang1"]];
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"已取消收藏"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        _flag = 0;
    }
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
